package com.zimi.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zimi.file.exception.InvalidExtensionException;
import com.zimi.file.service.FileInfoService;
import com.zimi.mapper.CompanyBusinessLicenceMapper;
import com.zimi.model.CompanyBusinessLicence;
import com.zimi.service.CompanyBusinessLicenceService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 营业执照信息 api
 *
 * @author lnintr
 * @since 2019-07-04
 */
@RestController
@RequestMapping("company-business-licence")
public class CompanyBusinessLicenceApi {
    @Autowired
    FileInfoService fileInfoService;
    @Autowired
    private CompanyBusinessLicenceMapper companyBusinessLicenceMapper;
    @Autowired
    private CompanyBusinessLicenceService companyBusinessLicenceService;

    @GetMapping("list")
    @ApiOperation("获取表格数据")
    public Object list(int page, int limit) {
        PageHelper.startPage(page, limit);
        List<CompanyBusinessLicence> list = companyBusinessLicenceMapper.selectList(new QueryWrapper<CompanyBusinessLicence>().orderByDesc());
        PageInfo<CompanyBusinessLicence> pageInfo = new PageInfo<CompanyBusinessLicence>(list);

        Map<String, Object> map = new HashMap<>();
        map.put("code", 0);
        map.put("data", list);
        map.put("count", pageInfo.getTotal());

        return map;
    }

    @PostMapping("select")
    @ApiOperation("模糊查询")
    public Object pageList(CompanyBusinessLicence companyBusinessLicence) {
        Map<String, Object> map = new HashMap<>();
        List<CompanyBusinessLicence> list = companyBusinessLicenceMapper.selectByNameAndCode(companyBusinessLicence);

        map.put("code", 0);
        map.put("data", list);
        return map;
    }

    @PostMapping("add")
    public Object add(CompanyBusinessLicence companyBusinessLicence) throws InvalidExtensionException {
        String tmpString = companyBusinessLicenceService.add(companyBusinessLicence);

        Map<String, Object> map = new HashMap<>();

        if (tmpString.length() > 0) {
            map.put("code", 1);
            map.put("msg", tmpString);
        } else {
            map.put("code", 0);
        }

        return map;
    }


}
